package com.xunji.standard.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author: xuqiang_imyours
 * @Date: 2025/7/9 16:14
 * @Description:
 **/
public class ExcelGeneratorUtil {

    public static void main(String[] args) {
        // 1. 创建工作簿和工作表
        Workbook workbook = new XSSFWorkbook(); // .xlsx格式
        Sheet sheet = workbook.createSheet("张三个人信息表");

        // 2. 创建样式（居中）
        CellStyle centerStyle = workbook.createCellStyle();
        centerStyle.setAlignment(HorizontalAlignment.CENTER); // 左对齐
        centerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        // 添加背景色
        centerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        centerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 创建加粗字体
        Font centerStyleBoldFont = workbook.createFont();
        centerStyleBoldFont.setBold(true);
        centerStyleBoldFont.setFontHeightInPoints((short) 14);  // 设置字号为14
        // 设置黑体
        centerStyleBoldFont.setFontName("黑体");
        centerStyle.setFont(centerStyleBoldFont);

        // 在创建 centerStyle 后添加边框样式
        CellStyle borderStyle = workbook.createCellStyle();
        borderStyle.setBorderTop(BorderStyle.THIN);
        borderStyle.setBorderBottom(BorderStyle.THIN);
        borderStyle.setBorderLeft(BorderStyle.THIN);
        borderStyle.setBorderRight(BorderStyle.THIN);
        borderStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        CellStyle centerBorderStyle = workbook.createCellStyle();
        centerBorderStyle.cloneStyleFrom(centerStyle); // 继承居中属性
        centerBorderStyle.setBorderTop(BorderStyle.THIN);
        centerBorderStyle.setBorderBottom(BorderStyle.THIN);
        centerBorderStyle.setBorderLeft(BorderStyle.THIN);
        centerBorderStyle.setBorderRight(BorderStyle.THIN);


        // 2. 创建样式（居中）
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.LEFT); // 左对齐
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        // 创建加粗字体
        Font titleFont = workbook.createFont();
        titleFont.setBold(true);
        titleFont.setFontHeightInPoints((short) 12);  // 设置字号为12
        // 设置黑体
        titleFont.setFontName("黑体");
        titleStyle.setFont(titleFont);

        // 设置列宽（单位：1/256个字符宽度）
        int columnWidth = 5;  // 列宽为5列
        for (int i = 0; i <= columnWidth; i++) {
            sheet.setColumnWidth(i, 30 * 256);
        }

        int currentRow = 0; // 当前行，0为起点

        Row row0 = sheet.createRow(currentRow);
        row0.setHeightInPoints(60); // 30点（约2厘米）
        // 填充第一行
        Cell cell0 = row0.createCell(0);
        cell0.setCellValue("张三" + "基本信息档案表");
        cell0.setCellStyle(centerStyle);
        // 合并单元格：合并第0行A1到D1
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 5));
        currentRow++;
        Row row1 = sheet.createRow(currentRow);
        Cell cell10 = row1.createCell(0);
        cell10.setCellValue("姓名：");
        cell10.setCellStyle(titleStyle);

        row1.createCell(1).setCellValue("张三");
        Cell cell12 = row1.createCell(2);
        cell12.setCellValue("性别：");
        cell12.setCellStyle(titleStyle);
        row1.createCell(3).setCellValue("男");
        Cell cell14 = row1.createCell(4);
        cell14.setCellValue("身份证号：");
        cell14.setCellStyle(titleStyle);
        row1.createCell(5).setCellValue("340123199333333333");
        row1.setRowStyle(titleStyle);
        currentRow++;
        Row row2 = sheet.createRow(currentRow);
        Cell cell20 = row2.createCell(0);
        cell20.setCellValue("出生日期：");
        cell20.setCellStyle(titleStyle);

        row2.createCell(1).setCellValue("1993年8月24");
        Cell cell22 = row2.createCell(2);
        cell22.setCellValue("民族：");
        cell22.setCellStyle(titleStyle);

        row2.createCell(3).setCellValue("汉");
        Cell cell24 = row2.createCell(4);
        cell24.setCellValue("文化程度：");
        cell24.setCellStyle(titleStyle);
        row2.createCell(5).setCellValue("本科");
        currentRow++;

        addBlankRow(sheet, currentRow, columnWidth, centerStyle);
        currentRow++;

        Row row3 = sheet.createRow(currentRow); // 联系方式
        row3.setHeightInPoints(60);
        Cell cell3 = row3.createCell(0);
        cell3.setCellValue("联系方式");
        cell3.setCellStyle(centerStyle);
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 5)); // 合并单元格
        currentRow++;
        Row row4 = sheet.createRow(currentRow);
        Cell cell40 = row4.createCell(0);
        cell40.setCellValue("登记时间");
        cell40.setCellStyle(titleStyle);
        Cell cell41 = row4.createCell(1);
        cell41.setCellValue("号码");
        cell41.setCellStyle(titleStyle);

        Cell cell42 = row4.createCell(2);
        cell42.setCellValue("类型");
        cell42.setCellStyle(titleStyle);
        Cell cell43 = row4.createCell(3);
        cell43.setCellValue("状态");
        cell43.setCellStyle(titleStyle);
        Cell cell44 = row4.createCell(4);
        cell44.setCellValue("运营商");
        cell44.setCellStyle(titleStyle);
        currentRow++;
        Row row5 = sheet.createRow(currentRow);
        row5.createCell(0).setCellValue("2023-07-09");
        row5.createCell(1).setCellValue("13888888888");
        row5.createCell(2).setCellValue("手机");
        row5.createCell(3).setCellValue("正常");
        row5.createCell(4).setCellValue("移动");
        currentRow++;

        addBlankRow(sheet, currentRow, columnWidth, centerStyle);
        currentRow++;

        Row row6 = sheet.createRow(currentRow); // 车辆信息
        row6.setHeightInPoints(60);
        Cell cell6 = row6.createCell(0);
        cell6.setCellValue("车辆信息");
        cell6.setCellStyle(centerStyle);
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 5));
        currentRow++;
        Row row7 = sheet.createRow(currentRow);
        row7.createCell(0).setCellValue("登记时间");

        row7.createCell(1).setCellValue("车辆品牌");
        row7.createCell(2).setCellValue("车辆类型");
        row7.createCell(3).setCellValue("车辆颜色");
        row7.createCell(4).setCellValue("车辆号码");
        currentRow++;
        Row row8 = sheet.createRow(currentRow);
        row8.createCell(0).setCellValue("2023-07-09");
        row8.createCell(1).setCellValue("保时捷");
        row8.createCell(2).setCellValue("轿车");
        row8.createCell(3).setCellValue("黑色");
        row8.createCell(4).setCellValue("京A88888");
        currentRow++;


        addBlankRow(sheet, currentRow, columnWidth, centerStyle);
        currentRow++;

        Row row9 = sheet.createRow(currentRow); // 不动产信息
        row9.setHeightInPoints(60);
        Cell cell9 = row9.createCell(0);
        cell9.setCellValue("不动产信息");
        cell9.setCellStyle(centerStyle);
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 5));
        currentRow++;
        Row row10 = sheet.createRow(currentRow);
        row10.createCell(0).setCellValue("登记时间");
        row10.createCell(1).setCellValue("城市");
        row10.createCell(2).setCellValue("位置");
        row10.createCell(3).setCellValue("面积");
        row10.createCell(4).setCellValue("共有人");
        currentRow++;
        Row row11 = sheet.createRow(currentRow);
        row11.createCell(0).setCellValue("2023-05-01 10:00:00");
        row11.createCell(1).setCellValue("上海");
        row11.createCell(2).setCellValue("上海徐汇");
        row11.createCell(3).setCellValue("1000");
        row11.createCell(4).setCellValue("5");
        currentRow++;


        addBlankRow(sheet, currentRow, columnWidth, centerStyle);
        currentRow++;

        Row row12 = sheet.createRow(currentRow); // 不动产信息
        row12.setHeightInPoints(60);
        Cell cellzhanghu = row12.createCell(0);
        cellzhanghu.setCellValue("账户信息");
        cellzhanghu.setCellStyle(centerStyle);
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 5));
        currentRow++;

        Row row13 = sheet.createRow(currentRow); // 账户信息
        row13.createCell(0).setCellValue("开户时间");
        row13.createCell(1).setCellValue("类型");
        row13.createCell(2).setCellValue("开户网点");
        row13.createCell(3).setCellValue("卡号");
        row13.createCell(4).setCellValue("状态");
        currentRow++;
        Row row14 = sheet.createRow(currentRow);
        row14.createCell(0).setCellValue("2020-08-08");
        row14.createCell(1).setCellValue("借记卡");
        row14.createCell(2).setCellValue("中国工商银行");
        row14.createCell(3).setCellValue("6228488888888888");
        row14.createCell(4).setCellValue("正常");

        // 添加所有框线

        // 在保存文件前添加边框遍历逻辑（放在 finally 前）
        for (int r = 0; r <= currentRow; r++) {
            Row row = sheet.getRow(r);
            if (row == null) continue;

            for (int c = 0; c <= columnWidth; c++) { // 遍历6列
                Cell cell = row.getCell(c);
                if (cell == null) {
                    cell = row.createCell(c); // 创建空单元格
                    cell.setCellStyle(borderStyle);
                } else {
                    if (cell.getCellStyle().equals(centerStyle)) {
                        cell.setCellStyle(centerBorderStyle);
                    } else if (cell.getCellStyle().equals(titleStyle)) {
                        cell.setCellStyle(titleStyle);
                    } else {
                        cell.setCellStyle(borderStyle);
                    }
                }
            }
        }

        // 保存文件
        try (FileOutputStream fos = new FileOutputStream("D:\\report.xlsx")) {
            workbook.write(fos);
            System.out.println("Excel文件生成成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void addBlankRow(Sheet sheet, int currentRow, int columnWidth, CellStyle centerStyle) {
        Row row3 = sheet.createRow(currentRow); // 联系方式
        row3.setHeightInPoints(25);
        Cell cell3 = row3.createCell(0);
        cell3.setCellValue("");
        //cell3.setCellStyle(centerStyle);
        sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, columnWidth)); // 合并单元格
    }

}
